﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using DacSanMienTayOBJ;
using System.Text;

public partial class UserControl_ManagerArticle : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    private void InsertArticle()
    {
        try
        {
            //int modID = (int)Session["ModId"];
            string title = Request["txtTitle"];
            string summary = Request["txtSummary"];
            string metakey = Request["txtMetaKeyword"];
            int topicID = int.Parse(Request["cbTopic"]);
            Random r = new Random();
            string s = r.Next(100000).ToString() + DateTime.Now.ToString() + "" + DateTime.Now.Ticks.ToString();
            string imageArticle = "";
            string img = "";
            if (fileImage.FileName != "")
            {
                img = s + fileImage.FileName;
                img = EncodeString(img) + ".jpg";
                string image = Server.MapPath("~/ImageArticle/" + img);
                fileImage.PostedFile.SaveAs(image);
                imageArticle = "ImageArticle/" + img;
            }
            string description = Request["txtDescription"];

            Article article = new Article();
            article.AdminID = 1;
            article.CreateDate = DateTime.Now;
            article.ContentArticle = description;
            article.ImageArticle = imageArticle;
            article.IsVisible = 1;
            article.Metakeyword = metakey;
            article.Priority = 1;
            article.Summary = summary;
            article.Title = title;
            article.TypeID = topicID;
           
            bool rs = ArticleBusiness.InsertArticle(article, ConnectionType.AdminConnection);
            if (rs)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Đã thêm bài viết mới')", true);
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Có lỗi khi thêm bài viết. Vui lòng liên hệ admin')", true);
            }
        }
        catch
        {

        }
    }

   
    private void UpdateArticle()
    {
        try
        {
           // int modID = (int)Session["ModId"];
            int articleID = int.Parse(Request["hdArticleID"]);
            Article art = ArticleBusiness.GetArticleDetail(articleID, ConnectionType.AdminConnection);
            string title = Request["txtTitle"];
            string summary = Request["txtSummary"];
            string metakey = Request["txtMetaKeyword"];
            int topicID = int.Parse(Request["cbTopic"]);
            Random r = new Random();
            string s = r.Next(100000).ToString() + DateTime.Now.ToString() + "" + DateTime.Now.Ticks.ToString();
            string imageArticle = art.ImageArticle;
            string img = "";
            if (fileImage.FileName != "")
            {
                img = s + fileImage.FileName;
                img = EncodeString(img) + ".jpg";
                string image = Server.MapPath("~/ImageArticle/" + img);
                fileImage.PostedFile.SaveAs(image);
                imageArticle = "ImageArticle/" + img;
            }
            string description = Request["txtDescription"];

            Article article = new Article();

          
            article.ContentArticle = description;
            article.ImageArticle = imageArticle;

            article.Metakeyword = metakey;

            article.Summary = summary;
            article.Title = title;
            article.TypeID = topicID;
          
            bool rs = ArticleBusiness.UpdateArticle(articleID, article, ConnectionType.AdminConnection);
            if (rs)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Đã cập nhật xong')", true);
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Lỗi cập nhật. Vui lòng liên hệ admin')", true);
            }
        }
        catch
        {

        }
    }
  
    private string EncodeString(string value)
    {
        Byte[] originalBytes;
        Byte[] encodedBytes;
        MD5 md5;
        md5 = new MD5CryptoServiceProvider();
        originalBytes = ASCIIEncoding.Default.GetBytes(value);
        encodedBytes = md5.ComputeHash(originalBytes);
        return BitConverter.ToString(encodedBytes);
    }
    protected void cmdAddArticle_Click(object sender, EventArgs e)
    {
        InsertArticle();
    }
    protected void cmdUpdateArticle_Click(object sender, EventArgs e)
    {
        UpdateArticle();
    }
}